/*
 * @Author: c
 * @Date: 2024-01-08 07:20:59
 * @LastEditTime: 2024-03-30 05:06:23
 * @LastEditors: c
 * @Description: 歌手-控制器
 * @FilePath: \music-web-backend\src\core\music\artist\artist.controller.ts
 * https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { Body, Controller, Delete, Get, Param, ParseIntPipe, Post, Put, Query, Req, Res, UploadedFile, UseInterceptors } from '@nestjs/common';
import { FileInterceptor } from '@nestjs/platform-express';
import { Request, Response } from 'express';
import { createReadStream } from 'fs';
import { join } from 'path';
import { SkipAuth } from 'src/core/system/auth/constants';
import { AlbumPageQuery } from '../album/dto/search-album.dto';
import { SongPageQuery } from '../song/dto/search-song.dto';
import { ArtistService } from './artist.service';
import { ArtistForm, ArtistPageQuery, FavoriteForm } from './dto';

@Controller('artist')
export class ArtistController {
  constructor(private readonly artistService: ArtistService) { }

  /**
  * 歌手-我收藏的歌手
  * @param pageDto 
  * @returns 
  */
  @Get("favorite")
  getFavorite(@Req() req, @Query() pageDto: ArtistPageQuery) {
    return this.artistService.myfavorite(req.user, pageDto);
  }

  /**
  * 歌手-歌手所有歌曲
  * @param pageDto 
  * @returns 
  */
  @SkipAuth()
  @Get("song")
  getArtistSongList(@Req() req, @Query() pageDto: SongPageQuery) {
    return this.artistService.getArtistSongList(req.user, pageDto);
  }
  
  /**
  * 歌手-歌手所有专辑
  * @param pageDto 
  * @returns 
  */
  @SkipAuth()
  @Get("album")
  getArtistAlbumList(@Req() req, @Query() pageDto: AlbumPageQuery) {
    return this.artistService.getArtistAlbumList(req.user, pageDto);
  }

  /**
    * 歌手-新增-我收藏的歌手
    * @param songForm 
    * @returns 
    */
  @Post("favorite")
  createFavorite(@Req() req, @Body() songForm: FavoriteForm) {
    return this.artistService.createfavorite(req.user, songForm);
  }

  /**
    * 歌手-删除-我收藏的歌手
    * @param songForm 
    * @returns 
    */
  @Delete("favorite")
  deletefavorite(@Req() req, @Body() songForm: FavoriteForm) {
    return this.artistService.deletefavorite(req.user, songForm);
  }
  /**
   * 歌手-分页查询
   * @param pageDto 
   * @returns 
   */
  @SkipAuth()
  @Get("page")
  findAll(@Query() pageDto: ArtistPageQuery) {
    return this.artistService.findAll(pageDto);
  }

  /**
   * 歌手-下拉查询
   * @param pageDto 
   * @returns 
   */
  @SkipAuth()
  @Get("options")
  listArtistOptions(@Query() pageDto: ArtistPageQuery) {
    return this.artistService.listArtistOptions(pageDto);
  }


  /**
   * 歌手-新增
   * @param pageDto 
   * @returns 
   */
  @Post()
  saveArtist(@Req() req: Request, @Body() artistForm: ArtistForm) {


    return this.artistService.saveArtist(req, artistForm); //必须return 不然报错终止整个运行的进程
  }


  /**
 * 歌手-单条表单数据
 * @param typeCode 
 * @returns 
 */
  @SkipAuth()
  @Get(":id/form")
  getArtistFormData(
    @Param('id', ParseIntPipe) id
  ) {
    return this.artistService.getArtistFormData(id);

  }


  /**
   * 歌手-修改
   * @param artistForm 
   * @returns 
   */
  @Put(':artistId')
  async updateArtist(@Req() req, @Param('artistId', ParseIntPipe) artistId: number, @Body() artistForm: ArtistForm) {

    return await this.artistService.updateArtist(artistId, artistForm); //必须return 不然报错终止整个运行的进程
  }

  /**
   * 歌手-批量删除
   * @param ids 
   * @returns 
   */
  @Delete(':ids')
  removeIds(@Param('ids') ids: string) {
    return this.artistService.deleteArtists(ids);
  }





  /**
   * 下载导入歌手的excel模板
   * @param ids 
   * @returns 
   */
  @Get('template')
  async downLoadExcelTemplate(@Res() res): Promise<any> {

    //第一种基于流下载
    const file = createReadStream(join(process.cwd(), 'dist/resources/歌手导入模板1.xlsx'));
    res.set({
      'Content-Type': 'application/json',
      'Content-Disposition': `attachment; filename=${encodeURI('歌手导入模板1.xlsx')}`,
    });

    file.pipe(res);

  }

  /**
   * 批量导入歌手
   * @param ids 
   * @returns 
   */
  @Post('_import')
  @UseInterceptors(FileInterceptor('file', { limits: { fieldSize: 10000 } })) //多文件使用@FileFieldsInterceptor
  async importArtists(@UploadedFile() file: Express.Multer.File) {

    return this.artistService.importArtists(file);
  }
  /**
   * 导出歌手Excel
   * @param ids 
   * @returns 
   */
  @Get('_export')
  async exportArtists(@Res() res: Response, @Query() artistPage: ArtistPageQuery) {
    return this.artistService.exportArtists(res, artistPage);
  }

}
